package com.foxmail.aroundme.boring.module.news.weixin;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.R;
import com.foxmail.aroundme.boring.bean.entry.WeiXinEntry;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.mutlitype.ItemViewProvider;
import com.socks.library.KLog;

/**
 * Created by gzl on 2/10/17.
 */

public class WeiXinProvider  extends ItemViewProvider<WeiXinEntry, WeiXinViewHolder> {

    private Context context;

    @NonNull
    @Override
    protected WeiXinViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {

        View view = inflater.inflate(R.layout.item_weixin, parent, false);

        context = inflater.getContext();

        return new WeiXinViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull WeiXinViewHolder holder, @NonNull WeiXinEntry weiXinEntry) {
        holder.weixinTitle.setText(weiXinEntry.WXTitle);
        holder.weixinSource.setText(weiXinEntry.WXSource);
        Glide.with(context)
                .load(weiXinEntry.WXFirstImg)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        if (BuildConfig.DEBUG) {
                            KLog.e(Config.TAG, "load weixin firstImg fail, onException = " + e.getMessage());
                        }

                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(holder.weixinImg);
    }
}
